package com.itheima.reggie.common;

import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import java.sql.SQLIntegrityConstraintViolationException;

@RestControllerAdvice
@Component
public class CommonExceptionResolver {

    @ExceptionHandler(SQLIntegrityConstraintViolationException.class)
    public R handlerException(SQLIntegrityConstraintViolationException e){
        e.printStackTrace();
        if (e instanceof SQLIntegrityConstraintViolationException){
            String message = e.getMessage();
            //Duplicate entry '111' for key 'idx_username'
            String[] split = message.split("'");
            return R.error(split[1]+"已存在");
        }
      return R.error("");
    }
}
